using System;
using System.Data;
using System.Data.Common;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace Microsoft.EntityFrameworkCore.Dm.Storage.Internal;

public class DmStringTypeMapping : StringTypeMapping
{
	internal const int UnicodeMax = 32767;

	private const int AnsiMax = 32767;

	private readonly int _maxSpecificSize;

	private readonly StoreTypePostfix? _storeTypePostfix;

	public DmStringTypeMapping([NotNull] string storeType, [CanBeNull] DbType? dbType, bool unicode = false, int? size = null, bool fixedLength = false, StoreTypePostfix? storeTypePostfix = null)
		: this(new RelationalTypeMappingParameters(new CoreTypeMappingParameters(typeof(string), (ValueConverter)null, (ValueComparer)null, (ValueComparer)null, (ValueComparer)null, (Func<IProperty, IEntityType, ValueGenerator>)null), storeType, GetStoreTypePostfix(storeTypePostfix, unicode, size), dbType, unicode, size, fixedLength, (int?)null, (int?)null))
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		_storeTypePostfix = storeTypePostfix;
	}

	protected DmStringTypeMapping(RelationalTypeMapping.RelationalTypeMappingParameters parameters)
			: base(parameters)
	{
		this._maxSpecificSize = DmStringTypeMapping.CalculateSize(parameters.Unicode, parameters.Size);
	}

	private static StoreTypePostfix GetStoreTypePostfix(StoreTypePostfix? storeTypePostfix, bool unicode, int? size)
	{
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		StoreTypePostfix? val = storeTypePostfix;
		if (!val.HasValue)
		{
			if (!unicode)
			{
				if (size.HasValue && size <= 32767)
				{
					return (StoreTypePostfix)1;
				}
				return (StoreTypePostfix)0;
			}
			if (size.HasValue && size <= 32767)
			{
				return (StoreTypePostfix)1;
			}
			return (StoreTypePostfix)0;
		}
		return val.GetValueOrDefault();
	}

	private static int CalculateSize(bool unicode, int? size)
	{
		if (!unicode)
		{
			if (!size.HasValue || !(size <= 32767))
			{
				return 32767;
			}
			return size.Value;
		}
		if (!size.HasValue || !(size <= 32767))
		{
			return 32767;
		}
		return size.Value;
	}

	public override RelationalTypeMapping Clone(string storeType, int? size)
	{
		return new DmStringTypeMapping(this.Parameters.WithStoreTypeAndSize(storeType, size, new StoreTypePostfix?(DmStringTypeMapping.GetStoreTypePostfix(this._storeTypePostfix, this.IsUnicode, size))));
	}

	// Token: 0x060001D9 RID: 473 RVA: 0x000096F0 File Offset: 0x000078F0
	public override CoreTypeMapping Clone(ValueConverter converter)
	{
		return new DmStringTypeMapping(this.Parameters.WithComposedConverter(converter));
	}

	protected override void ConfigureParameter(DbParameter parameter)
	{
		object value = parameter.Value;
		int? num = (value as string)?.Length;
		parameter.Size = ((value == null || value == DBNull.Value || (num.HasValue && num <= _maxSpecificSize)) ? _maxSpecificSize : 0);
	}

	protected override string GenerateNonNullSqlLiteral(object value)
	{
		return "'" + this.EscapeSqlLiteral((string)value) + "'";
	}
}
